package com.cykj.service.impl;

import com.cykj.dto.ResponseDTO;
import com.cykj.entity.MenuDO;
import com.cykj.mapper.MenuMapper;
import com.cykj.service.IMenuService;
import com.cykj.utils.TreeMenuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;


    @Override
    public ResponseDTO getTreeMenus(Integer roleId) {
        // 获取已分配菜单
        List<MenuDO> menuDOList = menuMapper.listMenuByRoleId(roleId);
        if (menuDOList == null || menuDOList.isEmpty()){
            return ResponseDTO.error("该管理员无任何权限");
        }
        // 获取所有菜单
        List<MenuDO> allMenuDOList = menuMapper.listMenu();
        // 构建树形菜单
        List<MenuDO> treeMenu = TreeMenuUtils.getAllocateMenu(allMenuDOList,menuDOList);
        return ResponseDTO.success("获取成功",treeMenu);
    }
}
